<?php

class LSourceMessageController extends BackEndController
{
	//public $layout='//layouts/column2';

	public function filters()
	{
            return array(
                    'rights', // perform access control for CRUD operations
            );
	}

	public function actionView($id)
	{
            $this->render('view',array(
                    'model'=>$this->loadModel($id),
            ));
	}

	public function actionCreate()
	{
            $model=new LSourceMessage;

            if(isset($_POST['LSourceMessage']))
            {
                $event = (int)$_POST['event'];
                $model->attributes=$_POST['LSourceMessage'];
                $category_id = $model->category;
                if($model->save()){
                    if ($event == 0){
                        $this->setNotice(
                            array(
                                array('title'=>Yii::t('language', 'Менеджер яыков'),'text'=>Yii::t('language', 'Метка успешно сохранена'),'type'=>'success'),
                            ));
                        $this->redirect(array('index'));
                    }
                    else {
                        $this->setNotice(
                                      array(
                                        array('title'=>Yii::t('language', 'Менеджер яыков'),'text'=>Yii::t('language', 'Метка успешно сохранена'),'type'=>'success'),
                                        array('title'=>Yii::t('language', 'Менеджер яыков'),'text'=>Yii::t('system', 'Можете продолжить заполнение.'),'type'=>'info'),
                                      )
                                );
                          $this->redirect(array('create','category_id'=>$category_id));  
                    }
                }

            }

            $this->render('create',array(
                    'model'=>$model,
            ));
	}

	public function actionUpdate($id)
	{
            $model=$this->loadModel($id);

            if(isset($_POST['LSourceMessage']))
            {
                    $model->attributes=$_POST['LSourceMessage'];
                    if($model->save()){
                            $this->setNotice(
                               array(
                                   array('title'=>Yii::t('language', 'Менеджер яыков'),'text'=>Yii::t('system', 'Информация успешно обновлена.'),'type'=>'success'),
                               ));
                            $this->redirect(array('index'));
                    }
            }

            $this->render('update',array(
                    'model'=>$model,
            ));
	}

	public function actionDelete($id)
	{
            if(Yii::app()->request->isPostRequest)
            {
                $this->loadModel($id)->delete();

                if(!isset($_GET['ajax']))
                        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
            }
            else
                throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	public function actionIndex()
	{
            $model=new LSourceMessage('search');
            $model->unsetAttributes();
            if(isset($_GET['LSourceMessage']))
                    $model->attributes=$_GET['LSourceMessage'];

            $this->render('index',array(
                    'model'=>$model,
            ));
	}
        


        public function loadModel($id)
	{
            $model=LSourceMessage::model()->findByPk($id);
            if($model===null)
                    throw new CHttpException(404,'The requested page does not exist.');
            return $model;
	}

	protected function performAjaxValidation($model)
	{
            if(isset($_POST['ajax']) && $_POST['ajax']==='lsource-message-form')
            {
                    echo CActiveForm::validate($model);
                    Yii::app()->end();
            }
	}
}